<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery.js"></script>
    <script src="../bootstrapt/js/bootstrap.js"></script>
    <link rel="stylesheet" href="../bootstrapt/css/bootstrap.css"/>
    <link rel="stylesheet" href="../css/simpleorder.css" type="text/css" media="all"/>
    <script>
        addEventListener("load", function () {
            setTimeout(hideURLbar, 0);
        }, false);

        function hideURLbar() {
            window.scrollTo(0, 1);
        }
    </script>
    <script>
        $(function () {
            //点击提交按钮
            $("#submit").click(function () {
                var v = $("#pay_way  option:selected").val();
                if (v == -1) {
                    alert("请选择支付方式！")
                } else {
                    var plan_stay_time = $("#plan_stay_time").val()    //计划住房时间
                    var plan_leave_time = $("#plan_leave_time").val()    //计划退房时间
                    var room_type_id = $("#room_type_id").val()//房间类型id
                    var pay_way = $("#pay_way  option:selected").val();//支付方式
                    var context = $("#context").val()   //留言
                    var total_price = $("#totalprice").val()    //总价格
                    $.ajax({
                        url: "/order/orderroom",
                        data: {
                            room_type_id: room_type_id,
                            plan_stay_time: plan_stay_time,
                            plan_leave_time: plan_leave_time,
                            pay_way: pay_way,
                            context: context,
                            total_price: total_price
                        },
                        success: function (data) {
                            if (data.indexOf('已经预定满了噢') == -1) {
                                if (pay_way == 0) {
                                    //线上支付，跳转支付宝
                                    alert("跳转支付宝咯，请在15分钟内完成付款哦，否则订单失效")
                                    //var newwindow = window.open("/pay/pay?out_trade_no=" + data + "&total_amount=" + total_price);
                                    //window.focus();
                                    location.href = "/pay/pay?out_trade_no=" + data + "&total_amount=" + total_price
                                } else if (pay_way == 1) {
                                    //线下支付
                                    alert("预定成功咯，请在入住时候付款")
                                    location.reload()
                                } else if (pay_way == 2) {
                                    //红包余额支付
                                    alert("红包给钱咯")
                                    location.reload()
                                }
                            } else {
                                alert(data)
                                location.reload()
                            }

                        }
                    })


                }

            })
            //点击取消按钮
            $("#abolish").click(function () {
                $("#content").slideToggle()
            })
        })

        function orderthis(aaa, name, price, id) {
            var plan_stay_time = $("#stay_time" + aaa).val()    //计划住房时间
            var plan_leave_time = $("#leave_time" + aaa).val()    //计划退房时间
            if (plan_stay_time == "") {
                alert("请选择入住时间")
            } else if (plan_leave_time == "") {
                alert("请选择退房时间")
            } else {
                $("#content").slideToggle()
                var time1 = Date.parse(new Date(plan_stay_time));
                var time2 = Date.parse(new Date(plan_leave_time));
                var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24));//两个日期相隔天数

                $("#plan_stay_time").val(plan_stay_time)
                $("#plan_leave_time").val(plan_leave_time)
                $("#roomname").val(name)
                $("#creditlevel").val("您是黄金会员享受0.8折")
                $("#finalprice").val((price * 0.8 * nDays).toFixed(2))
                $("#room_type_id").val(id)
            }
        }

        function change() {
            var v = $("#pay_way  option:selected").val();
            if (v == 0) {
                $("#totalprice").val(($("#finalprice").val() * 0.95).toFixed(2))
            } else if (v == 1) {
                $("#totalprice").val($("#finalprice").val())
            } else if (v == 2) {
                $("#totalprice").val($("#finalprice").val())
            }
        }
    </script>

</head>
<body>
<!-- title -->
<a href="/pay/tuikuan?out_trade_no=1571385605768&trade_no=2019101822001494551000040545">退款</a>
<!-- //title -->
<!-- content -->
<div class="porduct-order-agile" style="display: none; position: absolute; left: 400px;z-index: 2" id="content">
    <input type="hidden" id="room_type_id">
    <div class="form-group">
        <p>计划入住时间</p>
        <input type="text" id="plan_stay_time" name="name" placeholder="计划入住时间">
    </div>
    <div class="form-group">
        <p>计划退房时间 </p>
        <input type="text" id="plan_leave_time" placeholder="计划退房时间">
    </div>
    <div class="form-group">
        <p>预定留言</p>
        <input type="text" id="context" placeholder="若有需要，请留言">
    </div>

    <div class="order-list">
        <p>房间类型</p>
        <div class="main">
            <div class="form-order">
                <input type="text" class="form-control buttom"
                       style="border:0.5px solid black;position: relative;top: 10px;"
                       disabled="disabled" id="roomname"/>
            </div>
            <div class="form-order">
                <input type="text" class="form-control buttom"
                       style="border:0.5px solid black;position: relative;top: 10px;"
                       disabled="disabled" id="creditlevel"/>
            </div>
            <div class="form-order">
                <input type="text" class="form-control buttom"
                       style="border:0.5px solid black;position: relative;top: 10px;"
                       disabled="disabled" id="finalprice"/>
            </div>
        </div>
    </div>
    <div class="order-list">
        <p>请选择支付方式</p>
        <div class="main">
            <div class="form-order">
                <select class="form-control buttom" id="pay_way" onchange="change()">
                    <option value="-1">---支付方式---</option>
                    <option value="0">线上支付(可另享95折优惠)</option>
                    <option value="1">线下到店支付</option>
                    <option value="2">钱包余额支付</option>
                </select>
            </div>

            <div class="form-order">
                <input type="text" class="form-control buttom"
                       style="border:0.5px solid black;position: relative;top: 10px;"
                       disabled="disabled" id="totalprice"/>
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-primary" id="submit">提交预定</button>
    <button type="submit" class="btn btn-primary" id="abolish">取消</button>

</div>

<!--显示表格-->
<div style="position: absolute;z-index: 1">
    <table border="1">
        <thead>
        <th>room_type_id</th>
        <th>room_type_name</th>
        <th>room_type_picture</th>
        <th>room_type_price</th>
        <th>room_type_discount</th>
        <th>room_type_stock</th>
        <th>flag</th>
        <th>计划入住时间</th>
        <th>计划退房时间</th>
        <th>操作</th>
        <th>预定</th>
        </thead>
        <tbody>
        <tr th:each="roomtype:${allroom}" th:id="${roomtype.room_type_id}">
            <td th:text="${roomtype.room_type_id}"></td>
            <td th:text="${roomtype.room_type_name}"></td>
            <td th:text="${roomtype.room_type_picture}"></td>
            <td th:text="${roomtype.room_type_price}"></td>
            <td th:text="${roomtype.room_type_discount}"></td>
            <td th:text="${roomtype.room_type_stock}"></td>
            <td th:text="${roomtype.flag}"></td>
            <td><input type="date" th:id="stay_time+${roomtype.room_type_id}"></td>
            <td><input type="date" th:id="leave_time+${roomtype.room_type_id}"></td>
            <td><input type="button" value="加入购物车" id="addcart" th:onclick="addcart([[${roomtype.room_type_id}]])"></td>
            <td><input type="button" value="直接预定"
                       th:onclick="orderthis([[${roomtype.room_type_id}]],[[${roomtype.room_type_name}]],[[${roomtype.room_type_price}]],[[${roomtype.room_type_id}]])">
            </td>
        </tr>
        </tbody>
    </table>
    <a href="/cart/showcar">跳转购物车</a>
</div>
<script src="../js/jquery.js"></script>
<script>

    function addcart(aaa) {
        var plan_stay_time = $("#stay_time" + aaa).val()    //计划住房时间
        var plan_leave_time = $("#leave_time" + aaa).val()    //计划退房时间
        if (plan_stay_time == "") {
            alert("请选择入住时间")
        } else {
            if (plan_leave_time == "") {
                alert("请选择退房时间")
            } else {
                $.ajax({
                    url: "/cart/add",
                    data: {
                        room_type_id: aaa,
                        plan_stay_time: plan_stay_time,
                        plan_leave_time: plan_leave_time
                    },
                    success: function (data) {
                        alert(data)
                    }
                })
            }

        }

    }

</script>

</body>
</html>